package com.zhongxi.guoyuan.fragment.RewardCoinOrder;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.rewardCoinOrder.RewardOrderHasNoApplyAdapter;
import com.zhongxi.guoyuan.adapter.rewardCoinOrder.RewardOrderHasNoSendAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinStore.PostDefaultAddress;
import com.zhongxi.guoyuan.entity.rewardcoin.MallInfoResult;
import com.zhongxi.guoyuan.entity.rewardcoin.RewardCoinOrderInfo;
import com.zhongxi.guoyuan.entity.rewardcoin.RewardCoinOrderResult;
import com.zhongxi.guoyuan.url.Url;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class RewardOrderHasNoSendFragment extends BaseFragment implements AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2<ListView> {

    private PullToRefreshListView lvList;
    private List<RewardCoinOrderInfo> data = new ArrayList<>();
    private RewardOrderHasNoSendAdapter adapter;
    private ListView listView;
    private JSONObject info;
    private JSONObject shopInfo;
    private JSONObject orderCus;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_order_has_no_apply, container, false);
    }

    @Override
    protected void initView() {
        super.initView();
        lvList = (PullToRefreshListView) getView().findViewById(R.id.lvList);
        lvList.setMode(PullToRefreshBase.Mode.BOTH);
        adapter = new RewardOrderHasNoSendAdapter(context, data);
        lvList.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        super.initData();
        getOrder();
        TextView tv = new TextView(context);
        tv.setGravity(Gravity.CENTER);
        tv.setText("亲,暂时还没有数据哦!");
        tv.setTextSize(20);
        //当界面为空的时候显示的视图
        lvList.setEmptyView(tv);
    }

    /**
     * 获取待发货订单
     */
    private void getOrder() {
        String str = "&page=" + pageNum;
        getServer(Url.REWARD_TOSHIP, null, str, false, null, "get");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden)
        getOrder();
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            RewardCoinOrderResult result = gson.fromJson(response, RewardCoinOrderResult.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            if (pageNum == 1) {
                data.clear();
            }
            JSONObject obj = new JSONObject(response);
            if (result.getOrderInfo() != null && result.getOrderInfo().size() > 0) {
                List<RewardCoinOrderInfo> orderInfo = result.getOrderInfo();
                data.addAll(orderInfo);
                info = obj.optJSONObject("orderMirror");
                adapter.setInfo(info);

                shopInfo = obj.optJSONObject("shopInfo");

                adapter.setShopInfo(shopInfo);

                orderCus = obj.optJSONObject("orderCus");

                adapter.setOrderCus(orderCus);
            }
            adapter.notifyDataSetInvalidated();
            lvList.onRefreshComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            if (1000 == result.getCode()) {
                showShortToast("等待商家确认");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        lvList.setOnItemClickListener(this);
        lvList.setOnRefreshListener(this);
        listView = lvList.getRefreshableView();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position - 1 == adapter.getmExpandedMenuPos()) {
            adapter.setmExpandedMenuPos(-1);
        } else {
            adapter.setmExpandedMenuPos(position - 1);
        }
        adapter.notifyDataSetChanged();
        listView.setSelection(position);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum = 1;
        getOrder();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum++;
        getOrder();
    }
}
